package com.mida.app.report.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.mida.app.report.enumData.ReportType;
import com.mida.app.report.enumData.RequestStatus;
import com.mida.app.report.vo.*;
import com.mida.app.report.vo.request.PatientPageInfoVo;
import com.mida.bms.requestform.dto.RequestPageInfoDto;
import com.mida.bms.requestform.dto.ServiceInstanceInfoDto;
import com.mida.bms.requestform.request.RequestFormInfo;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: ReportListUtil
 * @Description: TODO
 * @Author: wang xiao le
 * @Date: 2021/8/7 17:19
 **/
public class ReportListUtil {

    //检查类型--1983代表ct
    public static final String EXAM_SERVICE_TYPE = "1983";

    /**
     * @return void
     * @Author WXL
     * @Description 申请单list-->报告list
     * @Date 2021/8/7
     * @Param [list, listTmp, personDetailInfosMap, serviceInstanceMap, apntRegistrationFormsMap, businessNameMap, dataSetSoidNameMap]
     **/
    public static void conventRequestFormToReport(List<DocReportListVo> list, List<RequestPageInfoDto> listTmp, Map<BigDecimal, PatientPageInfoVo> personDetailInfosMap, Map<BigDecimal, List<ServiceInstanceInfoDto>> serviceInstanceMap, HashMap<BigDecimal, List<ApntRegistrationForm>> apntRegistrationFormsMap, Map<BigDecimal, String> businessNameMap, Map<BigDecimal, String> dataSetSoidNameMap) {

        DocReportListVo docReportListVo;
        for (RequestPageInfoDto r : listTmp) {
            if (ObjectUtil.isEmpty(r) || ObjectUtil.isEmpty(r.getRequestSoid())) {
                continue;
            }
            docReportListVo = new DocReportListVo();
//                            BeanUtils.copyProperties(r,requestListInfo);
            BeanUtils.copyProperties(r, docReportListVo);
            if (r.getRequestSoid() != null) {
                docReportListVo.setRequestSoid(Long.valueOf(r.getRequestSoid().toString()));
            }
            //填充人信息
            if (ObjectUtil.isNotEmpty(personDetailInfosMap) && ObjectUtil.isNotEmpty(r.getCustomerSoid()) && ObjectUtil.isNotEmpty(personDetailInfosMap.get(new BigDecimal(r.getCustomerSoid())))) {
                PatientPageInfoVo patientPageInfoVo = personDetailInfosMap.get(new BigDecimal(r.getCustomerSoid()));
                BeanUtil.copyProperties(patientPageInfoVo, docReportListVo);
                docReportListVo.setGender(patientPageInfoVo.getGenderName());
                docReportListVo.setCustomerSoid(ObjectUtils.isEmpty(r.getCustomerSoid()) ? null : new BigDecimal(r.getCustomerSoid()));
                docReportListVo.setAge(StringUtils.isNotEmpty(patientPageInfoVo.getAge()) ? new BigDecimal(patientPageInfoVo.getAge()) : null);
            }
            if (r.getApntPersonSoid() != null) {
                docReportListVo.setApntPersonSoid(Long.valueOf(r.getApntPersonSoid().toString()));
            }
            if (r.getEmergencySign() != null) {
                docReportListVo.setEmergencySign(Long.valueOf(r.getEmergencySign().toString()));
                docReportListVo.setEmergencySignName(ObjectUtil.isNotEmpty(dataSetSoidNameMap) ? dataSetSoidNameMap.get(new BigDecimal(r.getEmergencySign())) : null);
            }
            if (r.getRequestStatus() != null) {
                docReportListVo.setRequestStatus(Long.valueOf(r.getRequestStatus().toString()));
                docReportListVo.setRequestStatusName(ObjectUtil.isNotEmpty(dataSetSoidNameMap) ? dataSetSoidNameMap.get(new BigDecimal(r.getRequestStatus())) : null);
            }
            List<ApntRegistrationForm> apntRegistrationForms = apntRegistrationFormsMap.get(new BigDecimal(r.getRequestSoid()));
            if (apntRegistrationForms.size() != 0
                    && apntRegistrationForms.get(0).getRegistrationFormSoid() != null) {
                if (apntRegistrationForms.get(0).getRegistrationFormSoid() != null) {
                    //登记标识
                    docReportListVo.setRegistrationFormSoid(Long.valueOf(apntRegistrationForms.get(0).getRegistrationFormSoid().toString()));
                }
                //设置登记号
                docReportListVo.setRegistrationNo(apntRegistrationForms.get(0).getRegistrationNo());
                if (apntRegistrationForms.get(0).getExaminationVisitSoid() != null) {
                    //上机号（检查号）
                    docReportListVo.setExaminationVisitSoid(Long.valueOf(apntRegistrationForms.get(0).getExaminationVisitSoid().toString()));
                }
                //上机号（检查号）
                docReportListVo.setOperatorSoid(Long.valueOf(String.valueOf(apntRegistrationForms.get(0).getOperatorSoid())));
                if (apntRegistrationForms.get(0).getApntPersonSoid() != null) {
                    //病人soid
                    docReportListVo.setApntPersonSoid(Long.valueOf(apntRegistrationForms.get(0).getApntPersonSoid().toString()));
                }
                if (apntRegistrationForms.get(0).getStPatientId() != null) {
                    //影像号
                    docReportListVo.setStPatientId(apntRegistrationForms.get(0).getStPatientId());
                }
            }

            if (r.getClinicCardType() != null) {
                docReportListVo.setClinicTypeName(ObjectUtil.isNotEmpty(dataSetSoidNameMap) ? dataSetSoidNameMap.get(r.getVisitScenceCode()) : null);
            }
            if (r.getRequestDept() != null) {
                docReportListVo.setBusinessUnitName(ObjectUtil.isNotEmpty(businessNameMap) ? businessNameMap.get(new BigDecimal(r.getRequestDept())) : null);
            }
            if (r.getRequestStatus() != null) {
                docReportListVo.setRequestStatus(Long.valueOf(r.getRequestStatus().toString()));
                docReportListVo.setRequestStatusName(ObjectUtil.isNotEmpty(dataSetSoidNameMap) ? dataSetSoidNameMap.get(new BigDecimal(r.getRequestStatus())) : null);
            }

            if (ObjectUtil.isNotEmpty(serviceInstanceMap)
                    && serviceInstanceMap.get(new BigDecimal(r.getRequestSoid())).size() > 0) {
//                                List<RequestServiceInfo> requestServiceInfoList = requestListInfo.getRequestServiceInfoList();
//                    List<RequestServiceInfo> requestServiceInfoList = r.getRequestServiceInfoList();
                List<ServiceInstanceInfoDto> serviceInstanceInfoDtos = serviceInstanceMap.get(new BigDecimal(r.getRequestSoid()));

//                                RequestServiceInfo requestServiceInfo = requestServiceInfoList.get(0);
                ServiceInstanceInfoDto requestServiceInfo = serviceInstanceInfoDtos.get(0);
                String examTypeName = null;
                if (ObjectUtils.isNotEmpty(dataSetSoidNameMap)) {
                    examTypeName = ObjectUtil.isNotEmpty(requestServiceInfo) && ObjectUtils.isNotEmpty(requestServiceInfo.getExamType()) ? dataSetSoidNameMap.get(requestServiceInfo.getExamType()) : null;
                }

                docReportListVo.setCtType(examTypeName);
                docReportListVo.setCtTypeSoid(Long.valueOf(requestServiceInfo.getExamType().toString()));
//                    List<RequestExamPartsInfo> listTmps =
//                            r.getRequestServiceInfoList().get(0).getRequestExamPartsInfoList();

//                    if (listTmps != null && listTmps.size() > 0) {
//                    docReportListVo.setOperateDateTime(listTmps.get(0).getCreateDate()); //设置检查时间
                List<ServiceInstanceVo> serviceInstanceVoList = new ArrayList<ServiceInstanceVo>();
                ServiceInstanceVo serviceInstanceVo = new ServiceInstanceVo();
                for (ServiceInstanceInfoDto rs : serviceInstanceInfoDtos) {
                    serviceInstanceVo = new ServiceInstanceVo();
                    if (rs.getExamType() != null) {
                        serviceInstanceVo.setCtType(ObjectUtil.isNotEmpty(dataSetSoidNameMap) && ObjectUtils.isNotEmpty(requestServiceInfo.getExamType()) ? dataSetSoidNameMap.get(requestServiceInfo.getExamType()) : null);
                        serviceInstanceVo.setCtTypeSoid(rs.getExamType().longValueExact());
                    }

                    if (ObjectUtils.isNotEmpty(rs.getExamParts())) {
                        serviceInstanceVo.setPart(ObjectUtil.isNotEmpty(dataSetSoidNameMap) && ObjectUtils.isNotEmpty(requestServiceInfo.getExamParts()) ? dataSetSoidNameMap.get(requestServiceInfo.getExamParts()) : null);
                    }

                    if (rs.getExamParts() != null) {
                        serviceInstanceVo.setPartSoid(Long.valueOf(rs.getExamParts().toString()));
                    }

                    if (rs.getMsdServiceInstanceSoid() != null) {
                        serviceInstanceVo.setMsdServiceInstanceSoid(rs.getMsdServiceInstanceSoid().longValueExact());
                    }
                    serviceInstanceVoList.add(serviceInstanceVo);
                }
                docReportListVo.setServiceInstanceVoList(serviceInstanceVoList);
            }
//                }

            list.add(docReportListVo);
        }
    }



    public static void convertExamination(List<DocReportListVo> list1, Map.Entry<Long, List<DocReportListVo>> longListEntry)
            throws IOException, ClassNotFoundException, CloneNotSupportedException {
        Long examinationVisitSoid = longListEntry.getKey();
        List<DocReportListVo> docReportList = longListEntry.getValue();

        //类型判断
        /*申请单       登记单
         *  1            1   ①
         *  2            1   ②
         *  1            2   ③
         *  2            2   ④
         * */
        //②③④
        if (docReportList.size() > 1) {
            //对申请单号和登记单号进行去重
            Set<Long> requestSoidSet = new LinkedHashSet<>();
            Set<Long> registrationSoidSet = new LinkedHashSet<>();
            docReportList.stream().filter(docReportListVo -> docReportListVo.getRequestSoid() != null
                    && docReportListVo.getRegistrationFormSoid() != null)
                    .forEach(docReportListVo -> {
                        Long requestSoid = docReportListVo.getRequestSoid();
                        Long registrationFormSoid = docReportListVo.getRegistrationFormSoid();
                        registrationSoidSet.add(registrationFormSoid);
                        requestSoidSet.add(requestSoid);
                    });

            DocReportListVo docReportListVo1 = new DocReportListVo();
            DocReportListVo docReportListVo = docReportList.get(0);
            BeanUtils.copyProperties(docReportListVo, docReportListVo1);
            //申请单号和登记单号去重之后进行类型判断
            //④
            if (requestSoidSet.size() > 1 && registrationSoidSet.size() > 1) {
                docReportListVo1.setRequisitions(requestSoidSet.size());
                docReportListVo1.setRegistrations(registrationSoidSet.size());
                //将符合此类型的list存下
                if (!ObjectUtils.isEmpty(docReportList)) {
                    docReportListVo1.setDocReportListVos(docReportList);

                    List<DocReportListVo> docReportListVos = ObjectUtil.cloneByStream(docReportList);
                    //以登记号分组，存下用来展示的数据
                    Map<Long, List<DocReportListVo>> collect =
                            docReportListVos.stream().collect(Collectors.groupingBy(DocReportListVo::getRegistrationFormSoid));
                    docReportListVo1.setDataMap(collect);
                }
                //拼接检查部位
                splicingTypeAndSite(docReportList, docReportListVo1);

                list1.add(docReportListVo1);
                //②
            } else if (requestSoidSet.size() > 1) {
                docReportListVo1.setRequisitions(requestSoidSet.size());
                //将符合此类型的list存下
                if (!ObjectUtils.isEmpty(docReportList)) {
                    docReportListVo1.setDocReportListVos(docReportList);
                }
                //拼接检查类型和检查部位
                splicingTypeAndSite(docReportList, docReportListVo1);
                list1.add(docReportListVo1);
                //③
            } else if (registrationSoidSet.size() > 1) {
                docReportListVo1.setRegistrations(registrationSoidSet.size());
                //将符合此类型的list存下
                if (!ObjectUtils.isEmpty(docReportList)) {
                    docReportListVo1.setDocReportListVos(docReportList);
                }
                //拼接检查类型和检查部位
                splicingTypeAndSite(docReportList, docReportListVo1);
                list1.add(docReportListVo1);
            }
        }
        //①
        if (docReportList.size() <= 1) {
            for (DocReportListVo reportListVo : docReportList) {
                list1.add(reportListVo);
            }
        }
    }

    /**
     * @return void
     * @Author WXL
     * @Description 申请单检查部位
     * @Date 2021/8/7
     * @Param [r, docReportListVo, listTmps]
     **/
    public static void convertRequestExamParts(RequestListInfo r, DocReportListVo docReportListVo, List<RequestExamPartsInfo> listTmps) {
        docReportListVo.setOperateDateTime(listTmps.get(0).getCreateDate()); //设置检查时间
        List<ServiceInstanceVo> serviceInstanceVoList = new ArrayList<ServiceInstanceVo>();
        ServiceInstanceVo serviceInstanceVo = new ServiceInstanceVo();
        for (RequestExamPartsInfo rs : listTmps) {
            serviceInstanceVo = new ServiceInstanceVo();
            //检查类型名称
            if (r.getRequestServiceInfoList().get(0).getExamTypeName() != null) {
                serviceInstanceVo.setCtType(r.getRequestServiceInfoList().get(0).getExamTypeName());
            }
            //检查类型
            if (r.getRequestServiceInfoList().get(0).getExamType() != null) {
                serviceInstanceVo.setCtTypeSoid(
                        Long.valueOf(r.getRequestServiceInfoList().get(0).getExamType().toString()));
            }
            //检查部位名称
            if (StringUtils.isNotEmpty(rs.getExamPartName())) {
                serviceInstanceVo.setPart(rs.getExamPartName());
            }
            //检查部位
            if (rs.getExamParts() != null) {
                serviceInstanceVo.setPartSoid(Long.valueOf(rs.getExamParts().toString()));
            }

            //检查服务实例标识
            if (r.getRequestServiceInfoList().get(0).getMsdServiceInstanceSoid() != null) {
                serviceInstanceVo.setMsdServiceInstanceSoid(Long.valueOf(r.getRequestServiceInfoList().get(0)
                        .getMsdServiceInstanceSoid().toString()));

            }
            serviceInstanceVoList.add(serviceInstanceVo);
        }
        docReportListVo.setServiceInstanceVoList(serviceInstanceVoList);
    }
    /**
     * @param list 检查单list
     * @Description: 合单，以检查号为key，合申请单号和登记单号 并展示有几份
     * @Date: Created in 9:54 2021/6/2
     */
    public static List<DocReportListVo> examMergeRequest(List<DocReportListVo> list) throws IOException,
            ClassNotFoundException, CloneNotSupportedException {
        List<DocReportListVo> list1 = new ArrayList<>();
        //根据检查单号进行分组
        Map<Long, List<DocReportListVo>> docReportMap =
                list.stream()
                        .filter(vo -> vo.getExaminationVisitSoid() != null)
                        .collect(Collectors.groupingBy(DocReportListVo::getExaminationVisitSoid));
        //按照检查号升序排序
        Map<Long, List<DocReportListVo>> longListMap = MapUtil.sortByKey(docReportMap, false);

        if (longListMap != null && longListMap.size() != 0) {
            //遍历map获取分组后的list
            for (Map.Entry<Long, List<DocReportListVo>> longListEntry : longListMap.entrySet()) {
                convertExamination(list1, longListEntry);
            }
        }
        return list1;
    }

    /**
     * @return void
     * @Author WXL
     * @Description 申请单数据转换报告数据
     * @Date 2021/8/7
     * @Param [list, r]
     **/
    public static void convertRequestInfoToReportData(List<DocReportListVo> list, RequestListInfo r) {
        DocReportListVo docReportListVo = new DocReportListVo();
        BeanUtils.copyProperties(r, docReportListVo);

        //申请单soid
        if (r.getRequestSoid() != null) {
            docReportListVo.setRequestSoid(Long.valueOf(r.getRequestSoid().toString()));
        }

        //年龄
        if (r.getAge() != null) {
            docReportListVo.setAge(BigDecimal.valueOf(r.getAge()));
        }

        //病人soid
        if (r.getApntPersonSoid() != null) {
            docReportListVo.setApntPersonSoid(Long.valueOf(r.getApntPersonSoid().toString()));
        }

        //加急标识
        if (r.getEmergencySign() != null) {
            docReportListVo.setEmergencySign(Long.valueOf(r.getEmergencySign().toString()));
        }

        //加急名称
        if (r.getEmergencySignName() != null) {
            docReportListVo.setEmergencySignName(r.getEmergencySignName());
        }

        if (r.getApntRegistrationFormByrequestSoid().size() != 0
                && r.getApntRegistrationFormByrequestSoid().get(0).getRegistrationFormSoid() != null) {
            if (r.getApntRegistrationFormByrequestSoid().get(0).getRegistrationFormSoid() != null) {
                //登记标识
                docReportListVo.setRegistrationFormSoid(Long.valueOf(r
                        .getApntRegistrationFormByrequestSoid()
                        .get(0)
                        .getRegistrationFormSoid()
                        .toString()));
            }
            //设置登记号
            docReportListVo.setRegistrationNo(r
                    .getApntRegistrationFormByrequestSoid()
                    .get(0)
                    .getRegistrationNo()
            );
            //上机号（检查号）
            if (r.getApntRegistrationFormByrequestSoid().get(0).getExaminationVisitSoid() != null) {
                docReportListVo.setExaminationVisitSoid(Long.valueOf(r
                        .getApntRegistrationFormByrequestSoid()
                        .get(0).getExaminationVisitSoid()
                        .toString()
                ));
            }

            //检查标识
            docReportListVo.setOperatorSoid(Long.valueOf(String.valueOf(r
                    .getApntRegistrationFormByrequestSoid()
                    .get(0).getOperatorSoid())));

            //病人soid
            if (r.getApntRegistrationFormByrequestSoid().get(0).getApntPersonSoid() != null) {
                docReportListVo.setApntPersonSoid(Long.valueOf(r
                        .getApntRegistrationFormByrequestSoid()
                        .get(0)
                        .getApntPersonSoid()
                        .toString()
                ));
            }
            //影像号
            if (r.getApntRegistrationFormByrequestSoid().get(0).getStPatientId() != null) {
                docReportListVo.setStPatientId(r
                        .getApntRegistrationFormByrequestSoid()
                        .get(0)
                        .getStPatientId()
                );
            }
        }

        docReportListVo.setClinicTypeName(r.getVisitScence());
        docReportListVo.setBusinessUnitName(r.getRequestDeptName());
        //申请单状态
        if (r.getRequestStatus() != null) {
            docReportListVo.setRequestStatus(Long.valueOf(r.getRequestStatus().toString()));
        }
        //申请单状态名称
        docReportListVo.setRequestStatusName(r.getRequestStatusName());


        if (r.getRequestServiceInfoList() != null
                && r.getRequestServiceInfoList().size() > 0) {
            //获取检查服务实例
            ReportListUtil.converRequsetserviceInfo( r, docReportListVo);
        }

        list.add(docReportListVo);
    }

    @NotNull
    public static List<DocReportListVo> fillTestPvo(PageResultVo pvo, Page page, PageVo pageVo, List<DocReportListVo> list) {
        //从申请单中查找待书写list
        list = MockTool.requestData(page, list);
        initPageInfo(pvo,pageVo,1L,10L,1L);
        return list;
    }





    /**
     * @return com.mida.bms.requestform.request.RequestFormInfo
     * @Author WXL
     * @Description 初始化申请单查询条件
     * @Date 2021/8/7
     * @Param [vo]  报告List分页查询vo
     **/
    @NotNull
    public static RequestFormInfo initializeQueryConditionsToRequest(DocReportListPageVo vo) {
        return initializeQueryConditionsToRequest(vo,RequestStatus.REQUEST_PART_PASSED.code,
                RequestStatus.REQUEST_REVIEW_PASSED.code, RequestStatus.REQUEST_FIRST_WRITTEN.code,
                RequestStatus.REQUEST_PART_PASSED.code, RequestStatus.REQUEST_PART_FIRST_WRITTEN.code);
    }

    public static RequestFormInfo initializeQueryConditionsToRequest(DocReportListPageVo vo,Long... codes) {

        RequestFormInfo requestFormInfo = new RequestFormInfo();
        requestFormInfo.setPageSize(Integer.valueOf(vo.getPageSize().toString()));
        requestFormInfo.setPageStart(Integer.valueOf(vo.getCurrentPage().toString()));

        //初始化申请单状态
        List<String> requestStatus = insertStatusCondition(codes);
        requestFormInfo.setRequestStatus(requestStatus);
        requestFormInfo.setExamServiceType(EXAM_SERVICE_TYPE);

        requestFormInfo.setTimeType("");
        return requestFormInfo;
    }


    public static void initPageInfo(PageResultVo pvo, PageVo pageVo, long current, long pages2, long total) {
        initPageInfo(pvo,pageVo,current,pages2,total, ReportType.WRITE_REPORT_REQUEST.code);
    }
    public static void initPageInfo(PageResultVo pvo, PageVo pageVo, long current, long pages2,
                              long total,Long reportTypeCode) {
        pageVo.setCurrentPage(current);
        pageVo.setEveryPage(pages2);
        pageVo.setReportType(reportTypeCode);
        pageVo.setTotalCount(total);

        pvo.setPageVo(pageVo);
    }










    /**
     * @param list 登记单list
     * @Description: 合单，以登记号为key，合申请单号 并展示有几份
     * @Date: Created in 9:54 2021/6/2
     */
    public static List<DocReportListVo> registerMergeRequest(List<DocReportListVo> list) throws IOException,
            ClassNotFoundException, CloneNotSupportedException {
        List<DocReportListVo> list1 = new ArrayList<>();
        Map<Long, List<DocReportListVo>> docReportMap =
                list.stream()
                        .filter(vo -> vo.getExaminationVisitSoid() != null)
                        .collect(Collectors.groupingBy(DocReportListVo::getRegistrationFormSoid));
        //排序
        Map<Long, List<DocReportListVo>> longListMap = MapUtil.sortByKey(docReportMap, false);
        //处理申请单数据
        if (longListMap != null && longListMap.size() != 0) {
            for (Map.Entry<Long, List<DocReportListVo>> longListEntry : longListMap.entrySet()) {
                Long registrationFormSoid = longListEntry.getKey();
                List<DocReportListVo> docReportList = longListEntry.getValue();

                if (docReportList.size() > 1) {
                    Set<Long> requestSoidSet = new LinkedHashSet<>();
                    Set<Long> examinationVisitSoidSet = new LinkedHashSet<>();

                    docReportList.stream().filter(docReportListVo -> docReportListVo.getRequestSoid() != null &&
                            docReportListVo.getRegistrationFormSoid() != null)
                            .forEach(docReportListVo -> {
                                Long requestSoid = docReportListVo.getRequestSoid();
                                Long examinationVisitSoid = docReportListVo.getExaminationVisitSoid();
                                requestSoidSet.add(requestSoid);
                                examinationVisitSoidSet.add(examinationVisitSoid);
                            });

                    DocReportListVo docReportListVo1 = new DocReportListVo();
                    DocReportListVo docReportListVo = docReportList.get(0);
                    BeanUtils.copyProperties(docReportListVo, docReportListVo1);
                    if (requestSoidSet.size() > 1 && examinationVisitSoidSet.size() == 1) {
                        docReportListVo1.setRequisitions(requestSoidSet.size());
                        //将符合此类型的list存下
                        if (!ObjectUtils.isEmpty(docReportList)) {
                            docReportListVo1.setDocReportListVos(docReportList);
                        }
                        //拼接检查部位
                        splicingTypeAndSite(docReportList, docReportListVo1);
                        list1.add(docReportListVo1);
                    }
                }
                if (docReportList.size() <= 1) {
                    for (DocReportListVo reportListVo : docReportList) {
                        list1.add(reportListVo);
                    }
                }
            }
        }
        return list1;
    }

    /**
     * @param list 申请单list
     * @Description: 合单，并给第一条的list添加elements表示后面还有几个元素
     * @Date: Created in 9:54 2021/6/2
     */
    public static List<DocReportListVo> mergeRequest(List<DocReportListVo> list) {
        List<DocReportListVo> list1 = new ArrayList<>();
        Map<Long, List<DocReportListVo>> docReportMap =
                list.stream()
                        .filter(vo -> vo.getExaminationVisitSoid() != null)
                        .collect(Collectors.groupingBy(DocReportListVo::getExaminationVisitSoid));
        Map<Long, List<DocReportListVo>> longListMap = MapUtil.sortByKey(docReportMap, false);//按照检查号升序排序
        if (longListMap != null && longListMap.size() != 0) {
            for (Map.Entry<Long, List<DocReportListVo>> longListEntry : longListMap.entrySet()) {
                Long examinationVisitSoid = longListEntry.getKey();
                List<DocReportListVo> docReportList = longListEntry.getValue();
                if (docReportList.size() > 1) {
                    docReportList.get(0).setElements(docReportList.size());
                }
                for (DocReportListVo reportListVo : docReportList) {
                    list1.add(reportListVo);
                }
            }
        }
        return list1;
    }





    /**
     * @Description: 部位拼接
     * @Date: Created in 9:54 2021/6/7
     */
    public static DocReportListVo splicingTypeAndSite(List<DocReportListVo> docReportList, DocReportListVo docReportListVo)
            throws IOException, ClassNotFoundException, CloneNotSupportedException {

        StringBuffer parts = new StringBuffer();
        String part = null;
        for (DocReportListVo reportListVo : docReportList) {
            List<ServiceInstanceVo> serviceInstanceVoList = reportListVo.getServiceInstanceVoList();
            if (!CollectionUtils.isEmpty(serviceInstanceVoList)) {
                for (ServiceInstanceVo serviceInstanceVo : serviceInstanceVoList) {
                    part = serviceInstanceVo.getPart();
                    if (StringUtils.isNotEmpty(part)) {
                        parts.append(part).append("+");
                    }
                }
            }
        }
        if (!ObjectUtils.isEmpty(docReportListVo)) {
            List<ServiceInstanceVo> serviceInstanceVoList = docReportListVo.getServiceInstanceVoList();
            //深度序列化，深拷贝
            List<ServiceInstanceVo> clone = ObjectUtil.cloneByStream(serviceInstanceVoList);
            //去除后面的+号
            if (!CollectionUtils.isEmpty(clone)) {
                for (ServiceInstanceVo serviceInstanceVo : clone) {
                    if (parts.length() > 0) {
                        serviceInstanceVo.setPart(parts.substring(0, parts.length() - 1));
                    }
                }
            }

            docReportListVo.setServiceInstanceVoList(clone);
        }
        return docReportListVo;
    }






    /**
     * @return java.util.List<java.lang.String>
     * @Author WXL
     * @Description 添加报告list查询入参状态
     * @Date 2021/8/7
     * @Param []
     **/
    @NotNull
    public static List<String> insertStatusCondition(Long... codes) {
        List<String> requestStatus = new ArrayList<String>();
        for (Long code : codes) {
            requestStatus.add(code.toString());//申请单已检查
        }
        return requestStatus;
    }

    /**
     * @return void
     * @Author WXL
     * @Description 获取检查服务实例
     * @Date 2021/8/7
     * @Param [r, docReportListVo]
     **/
    public static void converRequsetserviceInfo( RequestListInfo r, DocReportListVo docReportListVo) {
        List<RequestServiceInfo> requestServiceInfoList = r.getRequestServiceInfoList();

        RequestServiceInfo requestServiceInfo = requestServiceInfoList.get(0);

        String examTypeName = requestServiceInfo.getExamTypeName();

        docReportListVo.setCtType(examTypeName);
        docReportListVo.setCtTypeSoid(Long.valueOf(r.getRequestServiceInfoList().get(0).getExamType().toString()));
        List<RequestExamPartsInfo> listTmps =
                r.getRequestServiceInfoList().get(0).getRequestExamPartsInfoList();

        if (listTmps != null && listTmps.size() > 0) {
            //申请单检查部位
            ReportListUtil.convertRequestExamParts(r, docReportListVo, listTmps);
        }
    }
}
